﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using MugenInjection.Exceptions;
using MugenInjection.Web.Interface;
using MugenInjection.Web.Settings;
using Should;

namespace MugenInjectionTest.Web
{
    [TestClass]
    public class ApplicationConfigurationTest : TestBase
    {
        [TestMethod]
        public void TestConfigurations()
        {
            using (var injector = GetInjector())
            {
                var applicationConfiguration = new ApplicationConfiguration(injector);
                applicationConfiguration.Injector.ShouldEqual(injector);
                
                //Add configuration
                var config = new TestConfig();
                applicationConfiguration.AddConfiguration(config);
                applicationConfiguration.Configurations.Count.ShouldEqual(1);
                applicationConfiguration.Configurations.Contains(config).ShouldBeTrue();
                config.IsLoaded.ShouldBeTrue();
                config.IsUnloaded.ShouldBeFalse();
                
                //Add duplicate
                TestUtils.Catch<InvalidSettingException>(() => applicationConfiguration.AddConfiguration(config));
                TestUtils.Catch<InvalidSettingException>(() => applicationConfiguration.AddConfiguration(new TestConfig()));
                applicationConfiguration.Configurations.Count.ShouldEqual(1);
                applicationConfiguration.Configurations.Contains(config).ShouldBeTrue();

                //Remove
                applicationConfiguration.RemoveConfiguration(config);
                applicationConfiguration.Configurations.Count.ShouldEqual(0);
                applicationConfiguration.Configurations.Contains(config).ShouldBeFalse();
                config.IsLoaded.ShouldBeTrue();
                config.IsUnloaded.ShouldBeTrue();

                //Remove not exist
                TestUtils.Catch<InvalidSettingException>(() => applicationConfiguration.RemoveConfiguration(config));   
            }        
        }
    }

    public class TestConfig : IConfiguration
    {
        #region Property

        public bool IsLoaded { get; private set; }
        public bool IsUnloaded { get; private set; }

        #endregion

        #region Implementation of IConfiguration

        /// <summary>
        /// Load current configuration.
        /// </summary>
        /// <param name="configuration"></param>
        public void Load(IApplicationConfiguration configuration)
        {
            IsLoaded = true;
        }

        /// <summary>
        /// Unload current configuration.
        /// </summary>
        /// <param name="configuration"></param>
        public void Unload(IApplicationConfiguration configuration)
        {
            IsUnloaded = true;
        }

        #endregion
    }
}